package com.qiaofang.service.impl;

import com.google.common.collect.Lists;
import com.qiaofang.common.constant.DelFlagEnum;
import com.qiaofang.common.constant.ExceptionEnum;
import com.qiaofang.common.exception.BusinessException;
import com.qiaofang.common.model.UiSelectBean;
import com.qiaofang.common.utils.StringUtil;
import com.qiaofang.constant.StatusEnum;
import com.qiaofang.entity.Company;
import com.qiaofang.entity.CompanyRelation;
import com.qiaofang.repository.CompanyRelationRepository;
import com.qiaofang.repository.CompanyRepository;
import com.qiaofang.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyRepository companyRepository;

    @Autowired
    private CompanyRelationRepository companyRelationRepository;

    @Override
    public List<UiSelectBean> listOptionCompany(Long type) {
        List<Object[]> companies = companyRepository.listOptionCompany(type);
        List<UiSelectBean> list = Lists.newArrayList();
        if (companies != null) {
            for (Object[] company : companies) {
                UiSelectBean uiSelectBean = new UiSelectBean();
                uiSelectBean.setValue(company[0]);
                uiSelectBean.setLabel(company[1]);
                list.add(uiSelectBean);
            }
        }
        return list;
    }

    @Override
    public void enableCompany(Long id) {
        Company company = companyRepository.findById(id)
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        company.setStatus(StatusEnum.Enable.getNo());
        companyRepository.save(company);
    }

    @Override
    public void disableCompany(Long id) {
        Company company = companyRepository.findById(id)
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        company.setStatus(StatusEnum.Disable.getNo());
        companyRepository.save(company);
    }

    @Override
    public void deleteCompany(Long id) {
        Company company = companyRepository.findById(id)
                .orElseThrow(() -> new BusinessException(ExceptionEnum.NOT_FIND_DB_DATA));
        if (StringUtil.equals(company.getStatus(), StatusEnum.Disable.getNo())) {
            company.setDelFlag(DelFlagEnum.DEL.getNo());
            companyRepository.save(company);
            //删除该分销公司的外部关系
            CompanyRelation companyRelation=companyRelationRepository.findByAgentId(id);
            companyRelation.setDelFlag(DelFlagEnum.DEL.getNo());
            companyRelationRepository.save(companyRelation);
        } else {
            throw new BusinessException(ExceptionEnum.STATUS_DISABLE_DELETE);
        }
    }

    @Override
    public void batchEnableCompany(List<Long> ids) {
        List<Company> companies = companyRepository.findAllById(ids);
        if (companies != null) {
            for (Company company : companies) {
                company.setStatus(StatusEnum.Enable.getNo());
            }
        }
        companyRepository.saveAll(companies);
    }

    @Override
    public void batchDisableCompany(List<Long> ids) {
        List<Company> companies = companyRepository.findAllById(ids);
        if (companies != null) {
            for (Company company : companies) {
                company.setStatus(StatusEnum.Disable.getNo());
            }
        }
        companyRepository.saveAll(companies);
    }

    @Override
    public void batchDeleteCompany(List<Long> ids) {
        List<Company> companies = companyRepository.findAllById(ids);
        if (companies != null) {
            for (Company company : companies) {
                //禁用状态才能删除
                if (StringUtil.equals(company.getStatus(), StatusEnum.Enable.getNo())) {
                    throw new BusinessException(ExceptionEnum.BATCH_DEL_AGENCY_COMPANY);
                }
                company.setDelFlag(DelFlagEnum.DEL.getNo());
            }
            //删除这批分销公司的外部关系
            List<CompanyRelation> companyRelations=companyRelationRepository.getCompanyRelationList(ids);
            for (CompanyRelation companyRelation : companyRelations){
                companyRelation.setDelFlag(DelFlagEnum.DEL.getNo());
            }
            companyRelationRepository.saveAll(companyRelations);
        }
        companyRepository.saveAll(companies);
    }
}
